package ;

import flash.display.Sprite;
import flash.display.Bitmap;
import flash.geom.Point;
import openfl.Assets;
import motion.Actuate;
import motion.easing.Linear;
import flash.events.MouseEvent;

/**
 * ...
 * @author test
 */

//TODO: This is, clearly, a test and therefore must be reviewed in depth.

//Simple enemy class that, by now, only makes them move and die when clicked.
class Enemy extends Sprite {
	//The enemy bitmap.
	//TODO: Change to proper animation class.
	private var bitmap:Bitmap;

	//The enemy speed.
	private var speed:Float;

	//The enemy target.
	private var target:Point;

	//Flag that indicates if enemy is during dying animation.
	private var dying:Bool;

	//Getters and setters - BEGIN
	private function getDying():Bool {
		return this.dying;
	}

	private function setDying(dying:Bool):Void {
		this.dying = dying;
	}
	//Getters and setters - END

	//Constructor... nothing else to say here.
	public function new() {
		super();
		this.x = Math.random () * 300;
		this.bitmap  = new Bitmap(Assets.getBitmapData("images/zombie.png"));
		this.speed = 50.0;
		this.target = new Point(this.x,480);
		this.addChild(this.bitmap);
		//Actuate.tween(this, (480 / this.speed), {x: this.target.x, y: this.target.y} ).ease(Linear.easeNone);
		this.addEventListener(MouseEvent.MOUSE_DOWN, this.onClick);
		this.visible = false;
	}

	public function init():Void {
		this.visible = true;
		this.alpha = 1;
		Actuate.tween(this, (480 / this.speed), {x: this.target.x, y: this.target.y} ).ease(Linear.easeNone);
	}
	
	//Method to destroy enemy.
	private function destroy():Void {
		this.x = 0;
		this.y = 0;
		//this.visible = false;
		this.setDying(false);
	}

	//Method bound to event MOUSE_DOWN.
	private function onClick(event:MouseEvent):Void {
		if (!this.getDying()) {
			this.setDying(true);
			Actuate.stop(this);
			Actuate.tween(this, 1, { alpha:0.0 } ).onComplete(this.destroy);
		}
	}
}